[% setvar title Object Classes %]
<div id="archive-notice">
    <h3>This file is part of the Perl 6 Archive</h3>
    <p>To see what is currently happening visit <a href="http://www.perl6.org/">http://www.perl6.org/</a></p>
</div>
<div class='pod'>
<a name='TITLE'></a><h1>TITLE</h1>
<p>Object Classes</p>
<a name='VERSION'></a><h1>VERSION</h1>
<pre>  Maintainer: Andy Wardley &lt;<a href='mailto:abw@kfs.org'>abw@kfs.org</a>&gt;
  Date: 11 Aug 2000
  Last Modified: 17 Aug 2000
  Mailing List: <a href='mailto:perl6-language-objects@perl.org'>perl6-language-objects@perl.org</a>
  Number: 95
  Version: 2
  Status: Developing</pre>
<a name='ABSTRACT'></a><h1>ABSTRACT</h1>
<p>This RFC proposes a syntax and semantics for defining object classes
in Perl 6.  It introduces the <code>class</code> keyword, which can be thought
of as a special kind of <code>package</code> which incorates the functionality
of the Class::Struct module while giving the compile time typo
checking and access optimisation of the <code>use fields</code> pragma.  This
object class is an extension of the <code>package</code> mechanism, not a
replacement for it.</p>
<a name='SYNOPSIS'></a><h1>SYNOPSIS</h1>
<p>NOTE: these and other examples assume Highlander Variables (RFC 9)
where '$mage', '@mage' and '%mage' all refer to different &quot;views&quot; of
the same variable, rather than different variables as in Perl 5.
Otherwise read '@mage' as '@$mage' and '%mage' as '%$mage'.</p>
<pre>    class Person;
    our $debug = 0;		    # class variable
    my ($name, $race, @aliases);    # object (instance) variables

    sub name {			    # specific accessor method
	if (@_) {
	    $name = shift;
	    print &quot;changed name to $name\n&quot;
		if $debug;
	}
	return $name;
    }

    package main;
    $Person-&gt;debug = 1;		    # activate debugging

    # EITHER
    my $mage = Person-&gt;new(  # use positional parameters
	&quot;Gandalf&quot;, 
	&quot;Istar&quot;, 
	[&quot;Mithrandir&quot;, &quot;Olorin&quot;, &quot;Incanus&quot;]
    );
    
    # OR
    my $mage = Person-&gt;new(  # use named parameters
	name    =&gt; &quot;Gandalf&quot;, 
	race    =&gt; &quot;Istar&quot;, 
	aliases =&gt; [&quot;Mithrandir&quot;, &quot;Olorin&quot;, &quot;Incanus&quot;]
    );

    # access via named attributes
    print &quot;name: $mage-&gt;name\n&quot;;    # calls name() method
    print &quot;race: $mage-&gt;race\n&quot;;    # optimised to direct access
    print &quot; aka: @mage-&gt;aliases\n&quot;; # ditto

    # use it like a list
    my ($name, $race, $aliases) = @mage;

    # use it like a hash, with keys returned in correct order
    my @keys = keys %mage;	    # 'name', 'race', 'aliases'</pre>
<a name='OVERVIEW'></a><h1>OVERVIEW</h1>
<p>When it comes to creating objects, a hash is the most common choice of
structure for blessing as it offers the most versatility.  However,
you can get faster access and use less memory by using a list.  The
downside is that you lose the ability to reference items by name.
A pseudo-hash provides the efficency of a list with the convenience of
hash.</p>
<pre>    my $ph = [ { one =&gt; 1, two =&gt; 2 }, 'foo', 'bar' ];
    print $ph-&gt;{one};		# 'foo'
    print $ph-&gt;[2];		# 'bar'</pre>
<p>It works just like a hash but be warned that you can't treat it like a
regular list without taking into account the an extra item, the hash
array reference, at the start of the list.</p>
<pre>    @$ph = ('wiz', 'waz');	# wrong!</pre>
<p>The <code>use fields</code> pragma relies on pseudo-hash magic to make it
possible to access array elements by name.  Furthermore, the compiler
is smart enough to optimise access to the fields into straight array
accesses.  It also validates field names for typo safety.  These are
both Good Things.  That's all it does, though.  You still have to
build your own object constructor which calls the fields::new()
method.</p>
<pre>    package Person;
    use fields qw( name race aliases );

    sub new     { 
	my $type = shift;
	my Person $self = fields::new(ref $type || $type);
	...
	return $self;
    }

    package main;
    my $mage = Person-&gt;new();
    $mage-&gt;{name} = &quot;Gandalf&quot;;</pre>
<p>You can provide accessor methods as wrappers around the fields, but
note that you then have to change your code to call the methods instead
of accessing the hash fields directly.</p>
<pre>    package Person;
    ...
    sub name    { ... }
    sub race    { ... }
    sub aliases { ... }

    package main;
    my $mage = Person-&gt;new();
    $mage-&gt;name(&quot;Gandalf&quot;);	    # changed from '$mage-&gt;{name}'</pre>
<p>The Class::Struct method goes one step better but also takes two steps
back.  On the plus side, it automatically builds a default constructor
method and accessor methods for your class.  On the negative side, you
gain an extra level of indirection to each item, losing the compile
time optimisation and typo checking.  Furthermore, you have to specify
your class using an unusual syntax to keep the parser happy.</p>
<pre>    package Person;
    use Class::Struct;
    struct Person =&gt; {
	name    =&gt; '$',		    # not as obvious as '$name'
        race    =&gt; '$',		    # etc...
	aliases =&gt; '@',
    };</pre>
<p>One nice thing about the Class::Struct approach is that you can provide
your own package subroutines which override the defaults.  Unfortunately
you're also paying the price of method calls every time you access an
attribute whether you've defined a custom accessor method or not.</p>
<p>The <code>class</code> keyword is proposed as a way to implement the
functionality of Class::Struct, with the compile time benefits
of the <code>use fields</code> pragma, using a natural and Perl-like syntax.
The above examples would translate to:</p>
<pre>    class Person;
    my ($name, $race, @aliases);</pre>
<p>A <code>class</code> can be thought of as a special kind of <code>package</code>, defining
a namespace for objects of a particular type.  A default constructor
method, new(), should be provided to create objects of a given class.
Positional parameters are automatically assigned to the object variables
in the order defined.</p>
<pre>    my $mage = Person-&gt;new(&quot;Gandalf&quot;, &quot;Istar&quot;, 
			  [&quot;Mithrandir&quot;, &quot;Olorin&quot;, &quot;Incanus&quot;]);</pre>
<p>Or named parameters can be used, looking more like a hash assignment.</p>
<pre>    my $mage = Person-&gt;new(name    =&gt; &quot;Gandalf&quot;,
			   race    =&gt; &quot;Istar&quot;, 
			   aliases =&gt; [&quot;Mithrandir&quot;, &quot;Olorin&quot;, &quot;Incanus&quot;]);</pre>
<p>In implementation terms the objects themselves behave much like
pseudo-hashes.  An object of this class looks like a list containing
three data items (two scalars and a list reference).  It also looks
like a hash containing three keys, 'name', 'race' and 'aliases' which
point to the data items.  It also looks like an object and has a
constructor and accessor methods automatically provided which can be
typo checked and optimised away into list offsets at compile time.</p>
<pre>    my $mage = Person-&gt;new();

    # access object via named accessors, optimised to list offsets
    $mage-&gt;name = &quot;Gandalf&quot;;
    $mage-&gt;race = &quot;Istar&quot;;
    $mage-&gt;aliases( [&quot;Mithrandir&quot;, &quot;Olorin&quot;, &quot;Incanus&quot;] );

    # ...or just like a hash (but with the keys in the right order)
    my @keys = keys %mage;	# 'name', 'race', 'aliases'
    my ($name, $race) = @mage{ 'name', 'race' };

    # ...or just like a list
    my @data = @mage;		# 'Gandalf', 'Istar', [ 'Mithrandir', ... ]
    my $name = $mage[0];</pre>
<p>This object should have the benefits of an array in allowing you to
get your data in and out quickly.  It should have the benefits of a
hash in allowing names to be given to fields, and with the added bonus
that hash keys would be stored and returned in the order defined so
that they align with the values in the list.  It should be magical in
the same way as existing pseudo-hashes are, allowing named accesses to
be typo checked and optimised away at compile time into list offsets.
It should also have the capability of Class::Struct that allows
accessor subroutines to be definined in the package which then override
the default accessors.</p>
<p>This mechanism should also allow class variables and methods to be
defined.  It should support single inheritance as per the <code>use base</code>
pragma and additionally provide a 'mixin' facility to allow complex
classes to be constructed from other component classes without the
implications of inheritance.</p>
<a name='DESCRIPTION'></a><h1>DESCRIPTION</h1>
<a name='Defining a class'></a><h2>Defining a class</h2>
<p>The new <code>class</code> keyword is proposed for defining an object class.
This can be thought of as a special kind of <code>package</code>.  Lexical
variables defined within the scope of a class declaration become
attributes (or data members) of that class.  The existing <code>my</code>
keyword indicates per-instance variables while <code>our</code> is used to
create class variables which are shared across all instances of a
given class</p>
<pre>    class Person;
    our $home  = 'Middle Earth';    # class variable
    my ($name, $race, @aliases);    # object (instance) variables</pre>
<p>The class definition would continue until the next <code>class</code> or
<code>package</code> declaration, or until the end of the current file.
Class definitions can be re-opened and extended.</p>
<pre>    class Person;	# define 'Person' class
    ...

    class Wizard;	# define 'Wizard' class
    ...

    class Person;	# further definition of 'Person' class
    ...

    package main;	# no longer defining any class
    ...</pre>
<p>Subroutines defined within the scope of a class declaration become
methods of that object class.  Subroutines may be prefixed by <code>my</code> to
indicate object methods or by <code>our</code> to indicate class methods.  The
leading <code>my</code> on subroutines should probably be optional.  All
subroutines not explicitly prefixed <code>our</code> would then be object
methods by implication.</p>
<pre>    class Foo;

    our sub foo {			# class method
	...
    }

    my sub bar {			# object method (explicit)
	...
    }

    sub baz {				# object method (implicit)
        ...			
    }</pre>
<a name='Creating objects of a class'></a><h2>Creating objects of a class</h2>
<p>A default class constructor method <code>new()</code> is created automatically
unless otherwise defined in the class.</p>
<pre>    my $mage = Person-&gt;new();</pre>
<p>The default behaviour for <code>new()</code> is to assign the parameters passed
to the object variables in the order defined within the class.</p>
<pre>    class Person;
    my ($name, $race, @aliases);

    package main;
    my $mage = Person-&gt;new(&quot;Gandalf&quot;, &quot;Istar&quot;, 
			  [&quot;Mithrandir&quot;, &quot;Olorin&quot;, &quot;Incanus&quot;]);</pre>
<p>Named parameters can also be used, treating the object more like a hash
array.</p>
<pre>    my $mage = Person-&gt;new(name    =&gt; &quot;Gandalf&quot;,
			   race    =&gt; &quot;Istar&quot;, 
			   aliases =&gt; [&quot;Mithrandir&quot;, &quot;Olorin&quot;, &quot;Incanus&quot;]);</pre>
<p>This would allow construction of sparse objects (i.e. in which only
some attributes are set) and hopefully make it easier to remember
attributes by name rather than position.</p>
<p>RFC 57, &quot;Subroutine prototypes and parameters&quot;, discusses named
parameters further.  Also see RFC 84, &quot;Replace =&gt; (stringifying comma)
with =&gt; (pair constructor)&quot; which proposes a solution as to how we
might differentiate between positional and named parameters in an
argument list.</p>
<a name='Accessing variables and methods'></a><h2>Accessing variables and methods</h2>
<p>Object variables are accessed using the familiar <code>-&gt;</code> operator.</p>
<pre>    print $mage-&gt;name, &quot; has &quot;, 
	  scalar @mage-&gt;aliases, &quot; aliases\n&quot;;</pre>
<p>When an accessor method is defined for a given name it will be called.
Otherwise, the access is optimised away to a direct variable access.
The attribute name is also typo checked, raising a compile time error
if the variable or method isn't defined.</p>
<pre>    print $mage-&gt;mane;		# error: no such Person member 'mane'</pre>
<p>Class variables are accessed in a similar way, using the class name as
the receiver.</p>
<pre>    class DBI;
    our $debug;
    ...

    package main;
    $DBI-&gt;debug = 1;</pre>
<p>Classes may define specific accessor methods or other general purpose
class methods which are called in the same way as for objects.</p>
<pre>    class DBI;
    our $debug;
    ...

    our sub debug : lvalue {	# wrapper around $debug class var
	...
    }

    our sub connect {		# new class method
	my $dbh = $class-&gt;new(...);
	...
    }

    package main;
    $DBI-&gt;debug = 1;
    my $dbh = DBI-&gt;connect(...);</pre>
<a name='Internal variables'></a><h2>Internal variables</h2>
<p>Within a class definition, all variables are lexically scoped.
Within an object method, for example, the object variables are clearly
visible unless masked by other lexical variables in a narrower scope.</p>
<pre>    class User;
    my ($id, $name, $email);
    our $domain = 'nowhere.com';

    sub summary {
	# new $email lexical masks object variable
	my $email = $email || &quot;$id@$domain&quot;;

	# modify local copy
	$email = &quot;&lt;a href=\&quot;mailto:$email\&quot;&gt;$email&lt;/a&gt;&quot;;

	return &quot;Name: $name\nEmail: $email\n&quot;;
    }</pre>
<p>The special read-only variable <code>$me</code> (or <code>$ME</code>, <code>$self</code>, <code>$this</code>,
etc.) should be implicity defined in the outermost object scope.
Object methods can explicitly reference their attributes (methods, or
variables if undefined) through this reference.  It is automatically
defined and does not need to be passed to methods as a parameter as in
Perl 5.</p>
<pre>    class Foo;
    my ($bar, $baz);

    sub mumble {
	my ($foo, $bar, $baz);      # mask object variables
	...
	$me-&gt;bar = 10;		    # sets object variable
	$me-&gt;baz = 20;		    # calls object method
    }

    sub baz : lvalue {
	...
    }</pre>
<p>Object variables (<code>my</code>) should be undefined or inaccessible to class
methods, along with the <code>$me</code> variable.  Class variables (<code>our</code>)
would be visible to all class and object methods.  The special
read-only class variable <code>$class</code> should also be defined and visible
to class and object methods alike.  This should also be an externally
accessible attribute allowing the type of any object to be easily
determined.</p>
<pre>    my $u = User-&gt;new('foo', 'Mr Foo');
    print $u-&gt;class;		    # prints &quot;User&quot;</pre>
<a name='Private Variables and Methods'></a><h2>Private Variables and Methods</h2>
<p>Class or object members which are prefixed with '_' should be
considered private and not accessible from outside the class
definition.  This is as per the existing <code>use fields</code> pragma.</p>
<pre>    class User;
    our $_user_cache = { };	    # private class variable
    my  $_password;		    # private object variable

    my ($id, $name, $email);	    # public attributes

    sub _encrypt {		    # private object method
	...
    }</pre>
<a name='Inheritance'></a><h2>Inheritance</h2>
<p>As with the existing <code>use base</code>/<code>use fields</code> pragmata, Perl 6
classes should support single, linear inheritance only.  Multiple
inheritance is generally more trouble than it's worth.</p>
<p>The <code>isa</code> keyword is proposed as syntactic sugar for the existing
<code>use base</code> pragma.</p>
<pre>    class Person;
    my ($name, $sex, $dob);

    class User isa Person;
    my ($id, $email);

    class Hacker isa User;
    my @cool_hacks;</pre>
<p>Each subclass inherits the attributes of its parents in defined order
from most generic (super) to most specific (sub).  The Hacker class
above then contains the attributes as if written:</p>
<pre>    class Hacker;
    my ($name, $sex, $dob);	    # inherited from Person via User
    my ($id, $email);		    # inherited from User
    my @cool_hacks;</pre>
<p>Attributes in base classes can be redefined by derived classes.  The
special read-only variable <code>$super</code> should be implicitly defined
within the object scope.  Through this, object methods can explicitly
access attributes of the parent class or classes.</p>
<pre>    class User;

    sub foo {
	print &quot;User foo\n&quot;;
    }

    class Hacker isa User;

    sub foo {
	print &quot;Hacker foo\n&quot;;
	$super-&gt;foo;
    }

    package main;

    my $h = Hacker-&gt;new();
    $h-&gt;foo();			    # prints: Hacker foo
                                    #         User foo</pre>
<p>The <code>super</code> attribute should be accessible as an external attribute
returning the name of the immediate parent class (it may actually
return a reference to the class object which then returns its name on
stringification).</p>
<pre>    my $h = Hacker-&gt;new();
    print $u-&gt;class;		    # prints &quot;Hacker&quot;
    print $u-&gt;super;		    # prints &quot;User&quot;</pre>
<p>The <code>isa</code> attribute should return a list of the self and parent
classes in most-specific to most-general order when called without any
arguments.  If a specific class name is specified then it should
return a boolean result indicating if the object is a member of that
class.</p>
<pre>    class Person;
    class User isa Person;
    class Hacker isa User;

    my $h = Hacker-&gt;new();
    print join(', ', @h-&gt;isa);	    # prints &quot;Hacker, User, Person&quot;
    if ($h-&gt;isa('Person')) {	    # true
	...
    }</pre>
<p>Similarly, the <code>can</code> method should return a list of attributes
(variables or methods) that the object supports, or a boolean result
for a specific test.  These should be returned in order defined.</p>
<pre>    class Foo;
    my $foo;
    sub foo { ... };                # wrapper method masks variable
    sub bar { ... };

    class Bar isa Foo;
    my $baz;

    package main;
    my $b = Bar-&gt;new();
    print join(', ', @b-&gt;can);	    # prints &quot;foo, bar, baz&quot;</pre>
<a name='Delegation, Aliasing and Mixins'></a><h2>Delegation, Aliasing and Mixins</h2>
<p>It should be possible for objects to create internal &quot;plumbing&quot; to
help with delegation and interaction with other objects.  One possible
solution would be to allow variable attributes to contain references
to other class or objects attributes that are then traversed
automatically when the attribute is accessed.</p>
<pre>    class Foo;
    our $foo;
    my $bar;
    sub baz { ... }

    class Bar;
    my $_foo = Foo-&gt;new(...);	    # private Foo object
    my $wiz = \$_foo-&gt;bar;	    # alias $wiz to $_foo object var
    my $waz = \$_foo-&gt;baz;	    # alias $waz to $_foo object method
    my $foobar = \$Foo-&gt;foo;	    # alias $foobar to Foo class var $foo

    package main;
    my $bar = Bar-&gt;new();
    $bar-&gt;foobar;		    # ==&gt; $Foo.foo
    $bar-&gt;wiz;			    # ==&gt; $_foo-&gt;bar
    $bar-&gt;waz;			    # ==&gt; $_foo-&gt;baz()</pre>
<p>It may be desirable to allow all attributes of another class or object to
be imported into another object namespace.  For example:</p>
<pre>    class User;
    my ($name);
    sub welcome { return &quot;Hello World\n&quot; };

    class Hacker;
    mixin User;
    my $cool_hacks = [];</pre>
<p>The 'Hacker' class is not derived from 'User' but contains a copy of the
declaration which is added to its own.  The User class is used as a 'Mixin',
so named because the definition literally gets mixed in to the enclosing
class.  Hacker is thus defined as if written:</p>
<pre>    class Hacker;
    my ($name);
    sub welcome { return &quot;Hello World\n&quot; };
    my $cool_hacks = [];</pre>
<p>It should also be possible to mixin the attributes of a particular object,
rather than a class.</p>
<pre>    class Helper;
    my $msg;
    sub help { return $msg };

    class Hacker;
    my $helper = Helper-&gt;new(&quot;Hello World\n&quot;);
    mixin $helper;		    # equiv. to: my $msg  = \$helper-&gt;msg
                                    #            my $help = \$helper-&gt;help
    package main;
    my $hacker = Hacker-&gt;new();
    print $hacker-&gt;help;	    # ==&gt; $hacker-&gt;helper-&gt;help, prints
                                    #    &quot;Hello World\n&quot;</pre>
<a name='Constructor Methods'></a><h2>Constructor Methods</h2>
<p>The default class constructor method, new(), should be available if
otherwise undefined in the class.  It should instantiate an empty
object (via <code>class::new()</code>), assign any parameters to object
variables and then call any per-object initialisation method, NEW()
(or _new()?).  Base class NEW() constructors should be called in
order.  Note that the <code>$class</code> variable should be correctly defined
in the base class (e.g. Person) to contain the name of the derived
class (User).</p>
<pre>    class Person;
    my ($name, $sex, $dob);

    sub NEW {
	die &quot;$class name not specified&quot; unless $name;
	$sex ||= 'unknown';
	$dob ||= 'unknown';
	print &quot;new Person  (name: $name)\n&quot;;
    }

    class User isa Person;
    our $domain = 'perl.org';
    my ($id, $email);

    sub NEW {
	die &quot;$class id not specified&quot; unless defined $id;
	$email ||= &quot;$<a href='mailto:id@domain'>id@domain</a>&quot;;
	print &quot;new User  (name: $name  email: $email)\n&quot;;
    }

    package main;

    # attributes are $name, $sex, $dob, $email;
    my $u = User-&gt;new('Larry Wall', undef, undef, 'lwall');
    print &quot;Name: &quot;, $u-&gt;name, &quot;\n&quot;;
    print &quot;DOB: &quot;, $u-&gt;dob, &quot;\n&quot;;
    print &quot;Email: &quot;, $u-&gt;email, &quot;\n&quot;;</pre>
<p>Output:</p>
<pre>    new Person  (name: Larry Wall)
    new User  (name: Larry Wall  email: <a href='mailto:lwall@perl.org'>lwall@perl.org</a>)
    Name: Larry Wall
    DOB: unknown
    Email: <a href='mailto:lwall@perl.org'>lwall@perl.org</a></pre>
<p>Note one severe limitation of this model.  We must provide constructor
arguments in exactly the right order to satisfy base class (Person)
attributes first, followed by the derived class (User) attributes.
This makes our base classes exceptionally fragile to change.  If we
want to add an attribute to a class then we run the risk of breaking
any classes that are derived from it.  For this reason, some form of
named parameterisation would be preferred (see RFC 57).  e.g.</p>
<pre>    my $u = User-&gt;new(id =&gt; 'lwall', name =&gt; 'Larry Wall');</pre>
<p>This allows the structure of classes to be changed at any time without
affecting existing code.  Furthermore, we can specify only the attributes
that we care to define and in any order.</p>
<a name='Other Magical Methods'></a><h2>Other Magical Methods</h2>
<p>The OLD() method is proposed to compliment the NEW() method, being
called immediately before the object is destroyed.  Each destructor
(we'll call them that for now) should be called in reverse order from
most specific (sub) class to most general (super).</p>
<p>The DISPATCH() method is proposed to allow methods to intercept all
accesses to attributes (i.e. variables or methods).</p>
<pre>    sub DISPATCH {
	my $attr = shift; 
	if ($attr eq 'open_doors') {
	    die &quot;I'm sorry Dave, I can't do that\n&quot;;
	}
	$me-&gt;$attr(@_);
    }</pre>
<p>The AUTOLOAD() (or perhaps DEFAULT()?) method can be defined to
intercept any accesses to undefined attributes.</p>
<pre>    sub AUTOLOAD {
	my $attr = shift;	    # not via $AUTOLOAD as in Perl 5
	...
    }</pre>
<p>Note that DISPATCH() or AUTOLOAD() methods would require compile time
access optimisations to be disabled.</p>
<p>The INSPECT() method (or something similar) should be called whenever
the object itself is evaluated rather than a specific attribute.  In
conjunction with Damian Conway's RFC 21, &quot;Replace <code>wantarray</code> with a
generic <code>want</code> function&quot;, the proposed want() function could be used
to return a view of the object in many different formats.  The default
INSPECT() method might look notionally like this:</p>
<pre>    sub INSPECT {
	# I know, I should be using switch and currying.... :-)
	if (want('HASH')) {
	    # return hash of current attributes and values 
	    return map { ( $_ =&gt; $me-&gt;get($_) ) } @me-&gt;can;
	}
	elsif (want('ARRAY')) {
	    # return list of values
	    return map { $me-&gt;get($_) } @me-&gt;can;
	}
	elsif (want('SCALAR')) {
	    # return self for copy by reference
	    return $me;
	}
	elsif (want('STRING')) {
	    return &quot;$class: &quot; .
		    join(', ', map { &quot;$_ =&gt; &quot; . $me-&gt;get($_) } @me-&gt;can);
	}
	...etc...
    }</pre>
<p>This would allow an object reference ($object in these example) to be
used in many different contexts and return sensible value(s).  For
example.</p>
<pre>    my %objhash = %object;	# copy attribs/values into hash
    my @values  = @object;	# copy values into list
    my $obj2 = $object;		# copy by reference
    print &quot;$object&quot;;		# stringification </pre>
<a name='Pragma: 'use class''></a><h2>Pragma: 'use class'</h2>
<p>In the above examples, we have assumed that the <code>class</code> keyword and
associated functionality are available by default.  We might prefer to
have to enable it explicitly via the <code>use class</code> pragma.</p>
<pre>    use class;
    class Person;
    ...</pre>
<p>Modules containing class definitions could be loaded as per usual.</p>
<pre>    use Person;</pre>
<p>However, we might want to apply a different search heuristic or
loading behaviour for class modules than for regular modules.  We
could use the <code>use class</code> pragma to acheive this special-case:</p>
<pre>    use class qw( Person );	# load 'Person' class, by clever means
    my $p = Person-&gt;new();</pre>
<a name='IMPLEMENTATION'></a><h1>IMPLEMENTATION</h1>
<p>An object can be represented as a list reference blessed into a
particular class.  Each class would require a singleton class object
(like a typeglob) which would contain a vtable mapping attribute names
to list positions.  It would also maintain references to class
variables and both class and object methods, all of which can be
shared by the instances of the class.</p>
<p>The compiler optimisation code would be very similar to what already
exists under the <code>use fields</code> pragma.  It may be necessary to explicitly
type class variable to allow the compiler to perform these optimisations
efficiently (or at all?)</p>
<pre>    my Person $mage = Person-&gt;new(...);</pre>
<a name='HISTORY'></a><h1>HISTORY</h1>
<a name='Version 2'></a><h2>Version 2</h2>
<p>Added SYNOPSIS, OVERVIEW and HISTORY sections.</p>
<p>Removed CAVEAT and ISSUES.</p>
<p>Greatly reduced the length and complexity of the main DESCRIPTION
section, moving many tangential subjects out to separate RFCs or the
bit bucket.</p>
<p>Added section on <code>use class</code> pragma.</p>
<p>Removed all reference to the accursed dot operator, using the familiar
<code>-&gt;</code> operator instead.</p>
<a name='REFERENCES'></a><h1>REFERENCES</h1>
<p>RFC  9: Highlander variables</p>
<p>RFC 21: Replace <code>wantarray</code> with a generic <code>want</code> function</p>
<p>RFC 57: Subroutine prototypes and parameters</p>
<p>RFC 84: Replace =&gt; (stringifying comma) with =&gt; (pair constructor)</p>
<p>Programming Perl (3rd Edition), Larry Wall, Tom Christiansen &amp; Jon
Orwant.  O'Reilly and Associates.  ISBN 0-596-00027-8.  Chapter 12 :
&quot;Objects&quot;, pages 331 - 346.</p>
</div>
